/*
 * @Author: NtyAaron
 * @Date: 2021-04-12 11:13:45
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-09 15:01:29
 * @Description: file content
 * @FilePath: \miniapp\dwq\pages\check\shipResult\shipResult.js
 */

import { getFerryData, getMCArrivalData, getMCDepartureData } from '../../../api/checkShip/checkShip'
import { _t } from '../../../i18n/base'
Page({
  data: {
    reqInfo: {},
    hkList: [],
    macArrivalList: [], //入港
    macDepartureList: [], //离港
    language: '',
    isNoData: false,
  },
  // 获取香港跨境渡轮服务
  async getHkFerryData(e) {
    let res = await getFerryData(e)
    if (res.code == 200) {
      this.setData({
        hkList: res.result,
      })
    } else {
      this.setData({
        isNoData: true,
      })
    }
  },
  // 获取澳门码头实时航班咨询(入港)
  async getMCArrivalData(e) {
    let res = await getMCArrivalData(e)
    if (res.code == 200) {
      this.setData({
        macArrivalList: res.result.arrival.item,
      })
    } else {
      this.setData({
        isNoData: true,
      })
    }
  },
  // 获取澳门码头实时航班咨询(离港)
  async getMCDepartureData(e) {
    let res = await getMCDepartureData(e)
    if (res.code == 200) {
      this.setData({
        macDepartureList: res.result.departure.item,
      })
    } else {
      this.setData({
        isNoData: true,
      })
    }
  },
  // 判断该调用哪个接口
  judgeGetData() {
    if (typeof this.data.reqInfo.local == 'number') {
      this.getHkFerryData(this.data.reqInfo)
    } else {
      if (this.data.reqInfo.arrdep == 1) {
        this.getMCArrivalData(this.data.reqInfo)
      } else {
        this.getMCDepartureData(this.data.reqInfo)
      }
    }
  },
  // 复制
  copyLinkHandle({ currentTarget }) {
    let type = currentTarget.dataset.type
    //香港海事
    let XGUrl = {
      zh: 'https://crossboundaryferryservices.mardep.gov.hk/sc/',
      tw: 'https://crossboundaryferryservices.mardep.gov.hk/tc/',
      en: 'https://crossboundaryferryservices.mardep.gov.hk/en/',
    }
    //澳门海事
    let AMUrl = {
      zh: 'https://m.marine.gov.mo/seawayScheduled/RealTimeSailing.aspx?type=dataArrivalXML&local=PE&lang=SC',
      tw: 'https://m.marine.gov.mo/seawayScheduled/RealTimeSailing.aspx?type=dataArrivalXML&local=PE',
      en: 'https://m.marine.gov.mo/seawayScheduled/RealTimeSailing.aspx?type=dataArrivalXML&local=PE&lang=ENG',
    }
    let url = ''
    if (type == 1) {
      url = XGUrl[this.data.language]
    } else if (type == 2) {
      url = AMUrl[this.data.language]
    }
    if (url == undefined) {
      wx.showToast({
        title: this.data.lang['当前没有该网址'],
        icon: 'none',
      })
    }
    wx.setClipboardData({
      data: url,
      success() {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  onLoad(e) {
    let language = wx.getStorageSync('language')
    let langStatus = wx.getStorageSync('lang')
    this.setData({
      language,
      langStatus,
    })
    if (e) {
      let reqInfo = JSON.parse(e.req)
      this.setData({
        reqInfo: reqInfo,
        lang: _t(),
      })
      wx.setNavigationBarTitle({
        title: this.data.lang['船期查询'],
      })
      this.judgeGetData()
    }
  },
})
